
import json
from sanic import Sanic
from sanic import response
from sanic.request import Request
from utils.mysqlDb import MySQLDb
import conf.secure as secure
from sanic.exceptions import NotFound
from sanic.errorpages import JSONRenderer
import multiprocessing
from utils.common import convert



workers = multiprocessing.cpu_count()
app = Sanic('spider')
app.config.update(secure.config)
app.config.FALLBACK_ERROR_FORMAT = "json"
robot_client = secure.config['wx_robot']



@app.before_server_start
async def setup_db(_app, loop):
    db_robot = MySQLDb(**robot_client, sanic=_app)
    _app.ctx.db_sms = db_robot


@app.exception(NotFound)
async def ignore_404s(request, exception):
    print(request, exception)
    return JSONRenderer(request, exception, False).render()


@app.post("/api/robot/notice")
async def msgNotice(req:Request):
    try:
        data:dict = req.json
    except Exception as e:
        data:dict = dict(req.form) or dict(req.args)
    # data = convert(data)
    print(f"获取的内容:{data}")

    return response.json({
        'status': 0
    })


if __name__ == '__main__':
    app.run('0.0.0.0', port=80, workers=workers)